#ifndef __ITERATIONS_OMP_H
#define __ITERATIONS_OMP_H

#include <omp.h>
#include <windows.h>
#include <conio.h>
#include "function.h"


long iterate_omp(long iterCount)
{
	int workTime=GetTickCount();

	double iterSum=0;
	printf("Beginning iteration...\n");

	int maxThreads = omp_get_max_threads();
	
	double* threadSum = new double[maxThreads];
	for (int i=0; i<maxThreads;i++)
	{
		threadSum[i]=0;
	}
	
	omp_set_num_threads(maxThreads);
	int threadID;
#pragma omp parallel shared(maxThreads, threadSum) private (threadID)
	{
		threadID = omp_get_thread_num();
		for (long i=threadID; i < iterCount; i += maxThreads)
			threadSum[threadID]+=func(i);
	}

	for (int i=0; i<maxThreads;i++)
	{
		iterSum += threadSum[i];
	}
	///////////////////////////////////////////////

	//Print message and exit
	printf("Iteration complete!\n");
	printf("\nResult = %f\n",iterSum);
	workTime=GetTickCount() - workTime;
	printf("Work time = %d\n",workTime);

	printf("\nPress any key to EXIT.\n");
	getch();
	return iterSum;	
}

#endif